const session = require('koa-session2')
const KoaLogger = require('koa-logger')
const KoaJson = require('koa-json')
// const KoaCompress = require('koa-compress')
// const serve = require('koa-static')
const Koa = require('koa')
// const path = require('path')
// const historyApiFallback = require('koa2-connect-history-api-fallback').default
const koaBody = require('koa-body')
const errorHandle = require('./middleware/errorHandle.js')
const router = require('./router/index.js')

const { http_port, keys, session_key, redis } = require('./config/config.js')

const app = new Koa()
app.keys = keys

app.use(KoaLogger())
// app.use(historyApiFallback({ whiteList: ['/api'] }))
const session_option = {
    key: session_key,
    signed: true
    // store: new Store()
}
if (redis) {
    const Store = require('./common/store.js')
    session_option.store = new Store()
}
app.use(session(session_option))
app.use(KoaJson())
// app.use(serve(path.join(__dirname, 'public')))
// app.use(KoaCompress({ threshold: 2048 }))
app.use(koaBody())
app.use(errorHandle())
app.use(router.routes())

app.listen(http_port, () => {
    console.log(`server listen on http://localhost:${http_port}`)
})
